from rest_framework.renderers import JSONRenderer


# 重构json化方法,想要生效就要setting配置东西
class MyJsonRenderer(JSONRenderer):

    def render(self, data, accepted_media_type=None, renderer_context=None):
        """
        需要做的操作就是将data的值中添加code/msg/data
        :param data:
        :param accepted_media_type:
        :param renderer_context:
        :return:
        """
        # zhu
        try:
            msg = data.pop('message')
        except:
            msg = '请求成功'
        try:
            code = data.pop('code')
        except:
            code = 0
        try:
            data = data['data']
        except:
            data = data

        res = {
            'code': code,
            'msg': msg,
            'data': data
        }
        return super().render(res)

